/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import EventEmitter from './eventEmitter3'

let countsum = 200000
function emit_test() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  emitterInstance.on('multi', (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }, this);
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.emit('multi', "这是multi第一次发送");
  }
  let end = Date.now();

  print(`eventEmitter3_emit: ${end - start} ms`);
  // print(state)
}

// Test obtaining the API -- listeners of an event listeners array to see 
// if both on binding and once binding are counted
function listeners_test() {
  let emitterInstance = new EventEmitter();
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let listener3 = () => {
  };
  emitterInstance.on('listenNum', listener1,this);
  emitterInstance.on('listenNum', listener2,this);
  emitterInstance.once('listenNum', listener3,this);
  let event = () => { }
  emitterInstance.once('event', event,this);
  emitterInstance.emit('event', "这是通过once绑定的事件第一次发送");
  let state = '状态信息:\r\n';
  let listenerListenNum
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    listenerListenNum = emitterInstance.listeners('listenNum');
  }
  let end = Date.now();
  const listeners = emitterInstance.listeners('event');
  state = state + "\r\n" + `event事件计数个数,预期为1个,实际为${listeners.length}`;
  if (listenerListenNum.length === 3) {
    state = state + "\r\n" + `listenNum事件计数个数,预期为3个,实际为${listenerListenNum.length},on和once绑定的listener都计数`;
  } else if (listenerListenNum.length === 2) {
    state = state + "\r\n" + `listenNum事件计数个数,预期为3个,实际为${listenerListenNum.length},on绑定的listener都计数,once绑定的不计数`;
  } else if (listenerListenNum.length === 1) {
    state = state + "\r\n" + `listenNum事件计数个数,预期为3个,实际为${listenerListenNum.length},once绑定的listener都计数,on绑定的不计数`;
  } else {
    state = state + "\r\n" + `listenNum事件计数个数,预期为3个,实际为${listenerListenNum.length},计数错误`;
  }
  if(listenerListenNum.length !== 3){
    throw new Error('eventEmitter3_listeners err')
  }
  print(`eventEmitter3_listeners: ${end - start} ms`);
  // print(state)
}

//API for testing and obtaining the number of listeners for a certain event---listenerCount
function listenerCount_test() {
  let emitterInstance = new EventEmitter();
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let listener3 = () => {
  };
  emitterInstance.on('listenNum', listener1);
  emitterInstance.on('listenNum', listener2);
  emitterInstance.once('listenNum', listener3);
  let state = '状态信息:\r\n';
  let count
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    count = emitterInstance.listenerCount('listenNum');
  }
  let end = Date.now();
  state = state + "\r\n" + `测试获取某个事件listeners个数API---listenerCount,预期为3个,实际为${count}`;
  if(count !== 3){
    throw new Error('eventEmitter3_listenerCount err')
  }
  print(`eventEmitter3_listenerCount: ${end - start} ms`);
  // print(state)
}



//Test adding event binding API -- addListener. First, add addListener and then send
//  the event addListener to see if you have received the addListener event callback
function addListener_test() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.addListener('addListener', () => {
      state = state + "\r\n" + `收到事件addListener回调`;
    },this);
  }
  let end = Date.now();
  emitterInstance.emit("addListener", "这是addListener第-次发送");
  print(`eventEmitter3_addListener: ${end - start} ms`);
  // print(state)
}

//View bound event names
function eventNames_test() {
  let emitterInstance = new EventEmitter();
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  emitterInstance.on('listenNum', listener1);
  emitterInstance.on('listenNum', listener2);
  emitterInstance.on('multi', (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }, '这是multi事件');
  emitterInstance.once('before', (data) => {
    state = state + "\r\n" + `收到事件before回调,数据为:${data}`;
  });
  let callback = (data) => {
    state = state + "\r\n" + `收到事件callback回调,数据为:${data}`;
  }
  emitterInstance.addListener('callback', callback);
  let state = '状态信息:\r\n';
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    state = emitterInstance.eventNames();
  }
  let end = Date.now();
  if (emitterInstance.eventNames().length !== 4) {
    throw new Error('eventEmitter3_eventNames err')
  }
  print(`eventEmitter3_eventNames: ${end - start} ms`);
  // print(state)

}


//Test unbinding event API---off
function off_test() {
  let emitterInstance = new EventEmitter();
  // let listener1 = () => {
  // };
  let listener1 = (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }
  let listener2 = () => {
  };
  emitterInstance.on('listenNum', listener1,this);
  emitterInstance.on('listenNum', listener2,this);
  let state = '状态信息:\r\n';
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.off('listenNum', listener1,this);
  }
  let end = Date.now();
  let count = emitterInstance.listenerCount('listenNum');
  if(count !== 1){
    throw new Error('eventEmitter3_off err')
  }
  print(`eventEmitter3_off: ${end - start} ms`);
}


function off_test_01() {
  let emitterInstance = new EventEmitter();
  // let listener1 = () => {
  // };
  let listener1 = (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }
  let listener2 = () => {
  };
  emitterInstance.on('listenNum', listener2,this);
  let state = '状态信息:\r\n';
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.on('listenNum', listener1,this);
    emitterInstance.off('listenNum', listener1,this);
  }
  let end = Date.now();
  emitterInstance.off('listenNum', listener1);
  let count = emitterInstance.listenerCount('listenNum');
  if (count !== 1) {
    throw new Error('eventEmitter3_on+off err')
  }
  print(`eventEmitter3_on+off: ${end - start} ms`);
}


// Test subscribing to event multi first, and then sending event multi multiple times to see 
// if you have received multiple multi event callbacks
function on_test() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let listener1 = (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.on('multi', listener1, this);
  }
  let end = Date.now();
  emitterInstance.emit('multi', "这是multi第一次发送");
  emitterInstance.emit('multi', "这是multi第二次发送");
  print(`eventEmitter3_on: ${end - start} ms`);
  // print(state)
}

function removeListener_test_01(){
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let obj1 = () => {
  };
  let obj2 = () => {
  };
  let obj3 = (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }
  let obj4 = () => {
  };
  emitterInstance.on('newListener', obj1,this);
  emitterInstance.on('newListener', obj2,this);
  emitterInstance.on('newListener', obj3,this);
  emitterInstance.on('newListener', obj4,this);
  const beforeCount2 = emitterInstance.listenerCount('newListener');
  const nameBefore3 = emitterInstance.eventNames();
  state =state + "\r\n" + `测试解除事件绑定API---removeListener,一次性添加多个同名事件指定解除某一个监听器回调接口,解除绑定的事件之前事件个数${beforeCount2},事件名称${JSON.stringify(nameBefore3)}}`;
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.removeListener('newListener', obj3,this);
  }
  let end = Date.now();
  const afterCount2 = emitterInstance.listenerCount('newListener');
  const nameAfter2 = emitterInstance.eventNames();
 state =state + "\r\n" + `测试解除事件绑定API---removeListener,一次性添加多个同名事件指定解除某一个监听器回调接口,解除绑定的事件之后事件个数${afterCount2},事件名称${JSON.stringify(nameAfter2)}}`;
  // print(state )
  if (afterCount2!== 3) {
    throw new Error('eventEmitter3_removeListener err')
  }
  print(`eventEmitter3_removeListener: ${end - start} ms`);
}


function removeListener_test_02(){
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let obj1 = () => {
  };
  let obj2 = () => {
  };
  let obj3 = (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }
  let obj4 = () => {
  };
  emitterInstance.on('newListener', obj1,this);
  emitterInstance.on('newListener', obj2,this);
  emitterInstance.on('newListener', obj3,this);
  emitterInstance.on('newListener', obj4,this);
  const beforeCount2 = emitterInstance.listenerCount('newListener');
  const nameBefore3 = emitterInstance.eventNames();
  state =state + "\r\n" + `测试解除事件绑定API---removeListener,一次性添加多个同名事件指定解除某一个监听器回调接口,解除绑定的事件之前事件个数${beforeCount2},事件名称${JSON.stringify(nameBefore3)}}`;
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.on('newListener', obj3,this);
    emitterInstance.removeListener('newListener', obj3,this);
  }
  let end = Date.now();
  const afterCount2 = emitterInstance.listenerCount('newListener');
  const nameAfter2 = emitterInstance.eventNames();
  state =state + "\r\n" + `测试解除事件绑定API---removeListener,一次性添加多个同名事件指定解除某一个监听器回调接口,解除绑定的事件之后事件个数${afterCount2},事件名称${JSON.stringify(nameAfter2)}}`;
  // print(state )
  if (afterCount2!== 3) {
    throw new Error('eventEmitter3_on+removeListener err')
  }
  print(`eventEmitter3_on+removeListener: ${end - start} ms`);
}


// Test unbinding all event APIs---removeAllListeners
function removeAllListeners_test() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let callback = (data) => {
    state = state + "\r\n" + `收到事件callback回调,数据为:${data}`;
  }
  emitterInstance.on('listenNum', listener1);
  emitterInstance.on('listenNum', listener2);
  emitterInstance.on('multi', (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }, '这是multi事件');
  emitterInstance.once('before', (data) => {
    state = state + "\r\n" + `收到事件before回调,数据为:${data}`;
  });
  emitterInstance.addListener('callback', callback);
  const names = emitterInstance.eventNames();
  state =state + "\r\n" + `测试获取所有事件绑定API---eventNames,解除所有绑定的事件之前所有事件名称是:${JSON.stringify(names)}`;
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.removeAllListeners();
  }
  let end = Date.now();
  const count22 = emitterInstance.listeners('listenNum').length;
  const count33 = emitterInstance.listeners('multi').length;
  const count44 = emitterInstance.listeners('before').length;
  const count55 = emitterInstance.listeners('callback').length;
  let total1 = count22 + count33 + count44 
  state =state + "\r\n" + `测试解除所有事件绑定API---removeAllListeners,解除所有绑定的事件之后事件个数${total1}`;
  if (total1 !== 0) {
    throw new Error('eventEmitter3_removeAllListeners err')
  }
  // print(state)
  print(`eventEmitter3_removeAllListeners: ${end - start} ms`);
}


function removeAllListeners_test_01() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let callback = (data) => {
    state = state + "\r\n" + `收到事件callback回调,数据为:${data}`;
  }

  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.on('listenNum', listener1);
    emitterInstance.on('listenNum', listener2);
    emitterInstance.on('multi', (data) => {
      state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
    }, '这是multi事件');
    emitterInstance.once('before', (data) => {
      state = state + "\r\n" + `收到事件before回调,数据为:${data}`;
    });
    emitterInstance.addListener('callback', callback);
    emitterInstance.removeAllListeners();
  }
  let end = Date.now();
  const count22 = emitterInstance.listeners('listenNum').length;
  const count33 = emitterInstance.listeners('multi').length;
  const count44 = emitterInstance.listeners('before').length;
  const count55 = emitterInstance.listeners('callback').length;
  let total1 = count22 + count33 + count44 
  state =state + "\r\n" + `测试解除所有事件绑定API---removeAllListeners,解除所有绑定的事件之后事件个数${total1}`;
  if (total1 !== 0) {
    throw new Error('eventEmitter3_moreadd+removeAllListeners err')
  }
  // print(state)
  print(`eventEmitter3_moreadd+removeAllListeners: ${end - start} ms`);
}

function moreadd_test(){
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let callback = (data) => {
    state = state + "\r\n" + `收到事件callback回调,数据为:${data}`;
  }
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.on('listenNum', listener1);
    emitterInstance.on('listenNum', listener2);
    emitterInstance.on('multi', (data) => {
      state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
    }, '这是multi事件');
    emitterInstance.once('before', (data) => {
      state = state + "\r\n" + `收到事件before回调,数据为:${data}`;
    });
    emitterInstance.addListener('callback', callback);
  }
  let end = Date.now();
  print(`eventEmitter3_moreadd: ${end - start} ms`);
}

// Test subscribing to event single once first, and then sending event single multiple times
//  to see if you have received multiple single event callbacks
function once_test() {
  let emitterInstance = new EventEmitter();
  emitterInstance.removeAllListeners();
  let state = '状态信息:\r\n';
  let start = Date.now();
  for (let i = 0; i < countsum; i++) {
    emitterInstance.once('single', (data) => {
      state = state + "\r\n" + `收到事件single回调,数据为:${data}`;
    }, this);
  }
  let end = Date.now();
  print(`eventEmitter3_once: ${end - start} ms`);
  emitterInstance.removeAllListeners();
  emitterInstance.emit('single', "这是single第一次发送");
  emitterInstance.emit('single', "这是single第二次发送");
  
  
  // print(state)
}
emit_test()
listeners_test()
listenerCount_test()
addListener_test()
eventNames_test()
off_test()
off_test_01()
on_test()
removeListener_test_01()
removeListener_test_02()
removeAllListeners_test()
removeAllListeners_test_01()
moreadd_test()
once_test()